package com.ruralflow.population.service.impl;

import java.util.List;
import com.ruralflow.common.utils.DateUtils;
import com.ruralflow.common.utils.SecurityUtils;
import com.ruralflow.common.utils.SequenceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruralflow.population.mapper.HukouMapper;
import com.ruralflow.population.domain.Hukou;
import com.ruralflow.population.service.IHukouService;

/**
 * 户口信息Service业务层处理
 * 
 * @author ruralflow
 * @date 2025-02-15
 */
@Service
public class HukouServiceImpl implements IHukouService 
{
    @Autowired
    private HukouMapper hukouMapper;

    @Autowired
    private SequenceUtils sequenceUtils;

    /**
     * 查询户口信息
     * 
     * @param hukouId 户口信息主键
     * @return 户口信息
     */
    @Override
    public Hukou selectHukouByHukouId(Long hukouId)
    {
        return hukouMapper.selectHukouByHukouId(hukouId);
    }

    /**
     * 查询户口信息列表
     * 
     * @param hukou 户口信息
     * @return 户口信息
     */
    @Override
    public List<Hukou> selectHukouList(Hukou hukou)
    {
        return hukouMapper.selectHukouList(hukou);
    }

    /**
     * 新增户口信息
     * 
     * @param hukou 户口信息
     * @return 结果
     */
    @Override
    public int insertHukou(Hukou hukou)
    {
        hukou.setCreateTime(DateUtils.getNowDate());
        hukou.setCreateBy(SecurityUtils.getUsername());
        hukou.setHukouCode(getNo());
        return hukouMapper.insertHukou(hukou);
    }

    /**
     * 修改户口信息
     * 
     * @param hukou 户口信息
     * @return 结果
     */
    @Override
    public int updateHukou(Hukou hukou)
    {
        hukou.setUpdateTime(DateUtils.getNowDate());
        hukou.setUpdateBy(SecurityUtils.getUsername());
        return hukouMapper.updateHukou(hukou);
    }

    /**
     * 批量删除户口信息
     * 
     * @param hukouIds 需要删除的户口信息主键
     * @return 结果
     */
    @Override
    public int deleteHukouByHukouIds(Long[] hukouIds)
    {
        String updateBy = SecurityUtils.getUsername();
        return hukouMapper.deleteHukouByHukouIds(hukouIds, updateBy);
    }

    /**
     * 删除户口信息信息
     * 
     * @param hukouId 户口信息主键
     * @return 结果
     */
    @Override
    public int deleteHukouByHukouId(Long hukouId)
    {
        String updateBy = SecurityUtils.getUsername();
        return hukouMapper.deleteHukouByHukouId(hukouId, updateBy);
    }

    @Override
    public Integer selectCountHukou() {
        return hukouMapper.selectCountHukou();
    }

    /**
     * 生成户口本登记编码
     *
     * @return 户口本登记编码
     */
    private String getNo() {
        // 获取用户id补0到五位
        Long userId = SecurityUtils.getUserId();
        String userIdStr = String.format("%05d", userId); // 补零到5位
        return sequenceUtils.createSequenceNumber("HUK" + userIdStr);
    }
}
